package com.geek.gkpush.framework.event.model;

import com.geek.gkpush.framework.event.EventMsgDispatch;
import com.geek.gkpush.framework.event.EventMsgHandler;
import com.geek.gkpush.framework.event.core.AbstractEventMsgHandler;
import com.geek.gkpush.framework.event.enums.PushStatusEnum;
import com.geek.gkpush.util.enums.status.StatusEnum;
import com.geek.gkpush.util.id.BizUtils;
import com.geek.gkpush.util.hash.HashUtils;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

/**
 * <h1>事件消息模型对象，构建此模型对象后，就可以直接交给 {@linkplain EventMsgDispatch} 去写入数据库了。</h1><br/>
 *
 * <h1><br/><a href="https://u.geekbang.org/subject/arch3/100614201">Java 业务架构实战营：https://u.geekbang.org/subject/arch3/100614201</a></h1><br/><h1><a href="https://gitee.com/ylimhhmily/BizArchPracticeCamp">Java 业务架构实战营代码地址：https://gitee.com/ylimhhmily/BizArchPracticeCamp</a></h1><br/><h1><a href="https://u.geekbang.org/subject/java4th/1001148?source=app_share">高级 Java 工程体系课 2023 版：https://u.geekbang.org/subject/java4th/1001148?source=app_share</a></h1><br/><h1><a href="https://time.geekbang.org/column/intro/100312101?tab=catalog">Dubbo 源码剖析与实战：https://time.geekbang.org/column/intro/100312101?tab=catalog</a></h1>
 *
 * @author hmilyylimh
 *         ^_^
 * @version 0.0.1
 *         ^_^
 * @date 2023/10/2
 *
 */
@Slf4j
@Setter
@Getter
@ToString(callSuper = true)
public class EventMsgInsertModel implements Serializable {

    private static final long serialVersionUID = 5364511485246328202L;

    /** <h2>{@linkplain AbstractEventMsgHandler} 实现类的 beanName。</h2> **/
    private String bizType;

    /** <h2>业务ID</h2> **/
    private String bizId;
    /** <h2>消息内容</h2> **/
    private String msgContent;

    /**
     * <h2>创建对象。</h2>
     *
     * @param msgContent
     * @param handler
     * @param bizId
     * @return
     */
    public static EventMsgInsertModel create(String msgContent,
                                             Class<? extends EventMsgHandler> handler,
                                             String bizId) {
        EventMsgInsertModel model = new EventMsgInsertModel();

        model.setMsgContent(msgContent);

        model.setBizType(StringUtils.uncapitalize(handler.getSimpleName()));
        model.setBizId(bizId);

        return model;
    }

    /**
     * <h2>将当前 {@linkplain EventMsgInsertModel} 对象转换为 {@linkplain EventMsgDTO} 对象。</h2>
     *
     * @return
     */
    public EventMsgDTO convert2EventMsgDTO() {
        EventMsgDTO eventMsgDTO = new EventMsgDTO();

        eventMsgDTO.setId(BizUtils.getEventMsgId());
        eventMsgDTO.setBizType(this.getBizType());
        eventMsgDTO.setBizId(this.getBizId());
        eventMsgDTO.setHashCode((long) HashUtils.hash(eventMsgDTO.getBizType() + eventMsgDTO.getBizId()));

        eventMsgDTO.setPushStatus(PushStatusEnum.INIT.getCode());
        eventMsgDTO.setPushResult(PushStatusEnum.INIT.getMsg());

        eventMsgDTO.setMsgContent(this.getMsgContent());

        eventMsgDTO.setStatus(StatusEnum.V.getCode());
        eventMsgDTO.setCreateTime(System.currentTimeMillis());

        return eventMsgDTO;
    }

    /**
     * <h2>转换为简单的字符串打印信息。</h2>
     *
     * @return
     */
    public String toSimpleString() {
        final StringBuilder sb = new StringBuilder("EventMsgModel{");
        sb.append(", bizType='").append(bizType).append('\'');
        sb.append(", bizId='").append(bizId).append('\'');
        sb.append('}');
        return sb.toString();
    }
}